﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CVSA.Suporte.Sinacor.BMFBovespa.Entidades;
using CVSA.Suporte.Sinacor.BMFBovespa.Dados;
using System.Globalization;


namespace CVSA.Suporte.Sinacor.BMFBovespa.Negocio
{
    public class SRM
    {
        private Dados.SRM  srm = new CVSA.Suporte.Sinacor.BMFBovespa.Dados.SRM();

        public List<Chamado> ObterDadosWS()
        {
            DateTime ultimoSincronismo = DateTime.Today.AddDays(-1);
            string ultimaAtualizacao = ultimoSincronismo.Day.ToString("00") + "/" + ultimoSincronismo.Month.ToString("00") + "/" + ultimoSincronismo.Year;// +" " + ultimoSincronismo.ToString("hh") + ":" + ultimoSincronismo.Minute.ToString("00") + ":" + ultimoSincronismo.Second.ToString("00") + " " + ultimoSincronismo.ToString("tt", CultureInfo.InvariantCulture);
            string Classification = string.Empty;
            //Classification += @"'Assigned Group'LIKE ""DO-CVSA%"" ";
            Classification += @"'Submit Date' >= """ + ultimaAtualizacao + @"""";
            return srm.getWorkOrdersWS(Classification);
        }

        public DataSet ObterDados_ds()
        {
            
            DateTime inicio = Convert.ToDateTime("20/06/2009");
            string s_inicio = inicio .Day.ToString("00") + "/" + inicio .Month.ToString("00") + "/" + inicio .Year;// +" " + inicio .ToString("hh") + ":" + inicio .Minute.ToString("00") + ":" + inicio .Second.ToString("00") + " " + inicio .ToString("tt", CultureInfo.InvariantCulture);
            string Classification = string.Empty;

            DateTime fim = Convert.ToDateTime("22/06/2010");
            string s_fim = fim.Day.ToString("00") + "/" + fim.Month.ToString("00") + "/" + fim.Year;// +" " + fim .ToString("hh") + ":" + fim .Minute.ToString("00") + ":" + fim .Second.ToString("00") + " " + fim .ToString("tt", CultureInfo.InvariantCulture);
            
            
            
            //Classification += @"'Assigned Group'LIKE ""DO-CVSA%"" ";

            Classification += @" 'Submit Date' >= """ + s_inicio + @"""";
            Classification += @" AND 'Submit Date' <= """ + s_fim + @"""";
            return srm.getWorkOrdersWS_ds(Classification);
        }

        public List<Entidades.SRM> ObterDadosODBC()
        {
         

            string query = @"SELECT WOI_WorkOrder.Work_Order_ID, WOI_WorkOrder.SRID, WOI_WorkOrder.Customer_Company, WOI_WorkOrder.Summary, WOI_WorkOrder.WO_Type_Field_01, WOI_WorkOrder.WO_Type_Field_02, WOI_WorkOrder.WO_Type_Field_03, WOI_WorkOrder.WO_Type_Field_04, WOI_WorkOrder.WO_Type_Field_05, WOI_WorkOrder.WO_Type_Field_10, WOI_WorkOrder.ASCHG, WOI_WorkOrder.ASGRP, WOI_WorkOrder.Submit_Date, WOI_WorkOrder.Status, WOI_WorkOrder.Last_Modified_Date, WOI_WorkOrder.Priority, WOI_WorkOrder.Completed_Date
                            FROM WOI_WorkOrder WOI_WorkOrder
                            WHERE (WOI_WorkOrder.ASGRP Like 'DO-CVSA%'  AND  WOI_WorkOrder.ASGRP NOT LIKE '%DEMANDAS%')"  ;

            return srm.getWorkOrderODBC(query);
        }

        public void SincronizarSRM()
        {
            DateTime UltimaAtualizacao = srm.UltimoSincronismo().AddDays(-1);

            string query = @"SELECT WOI_WorkOrder.Work_Order_ID, WOI_WorkOrder.SRID, WOI_WorkOrder.Customer_Company, WOI_WorkOrder.Summary, WOI_WorkOrder.WO_Type_Field_01, WOI_WorkOrder.WO_Type_Field_02, WOI_WorkOrder.WO_Type_Field_03, WOI_WorkOrder.WO_Type_Field_04, WOI_WorkOrder.WO_Type_Field_05, WOI_WorkOrder.WO_Type_Field_10, WOI_WorkOrder.ASCHG, WOI_WorkOrder.ASGRP, WOI_WorkOrder.Submit_Date, WOI_WorkOrder.Status, WOI_WorkOrder.Last_Modified_Date, WOI_WorkOrder.Priority, WOI_WorkOrder.Completed_Date
                            FROM WOI_WorkOrder WOI_WorkOrder
                            WHERE (WOI_WorkOrder.ASGRP Like 'DO-CVSA%'  AND  WOI_WorkOrder.ASGRP NOT LIKE '%DEMANDAS%') AND (WOI_WorkOrder.Last_Modified_Date >={ts '" + UltimaAtualizacao.ToString("yyyy-MM-dd") + " 00:00:00'})";

            List<Entidades.SRM > Chamados = srm.getWorkOrderODBC(query);
            
            srm.SincronizarSRM(Chamados);
        }

        public DateTime ultimoSincronismo()
        {
            return srm.UltimoSincronismo();
        }

        public DataTable ObterChamadosDB(string clause)
        {
            return srm.ObterDadosDB(clause);
        }

        public DataTable ObterChamadosAbertos()
        {
            return srm.ObterChamadosAbertos();
        }
    }
}
